package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.ListUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.ChuangZhiParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 黑米 on 2017/3/6.
 */
@Service
public class ChuangZhiCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(ChuangZhiCrawler.class);

    @Autowired
    private ChuangZhiParser chuangZhiParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.CHUANGZHI_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.CHUANGZHI_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "a.avatar>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.title>a";
    }

    /**
     * 频道名称是否同时是作者名称
     *
     * @return 是、否
     */
    @Override
    protected boolean isAuthorName() {
        return true;
    }

    @Override
    protected String listSelect() {
        return "div.article";
    }

    @Override
    protected String titleSelect() {
        return "div.info>div.top>a";
    }

    @Override
    protected String authorLink() {
        return null;
    }

    @Override
    protected String authorSelect() {
        return "div.title>a";
    }

    //http://tech2ipo.com/index/list?category=1&index=0&_=1488780741726  新闻&趣事
    //http://tech2ipo.com/index/list?category=1&index=1&_=1488780741727
    //http://tech2ipo.com/index/list?category=2&index=0&_=1488780741728  态度&观点
    //http://tech2ipo.com/index/list?category=4&index=0&_=1488780741729  公司&产业
    //http://tech2ipo.com/index/list?category=3&index=0&_=1488780741730  网络&文化
    //http://tech2ipo.com/index/list?category=5&index=0&_=1488780741731  前言科技
//http://chuang.pro/index/list?orderby=hot&index=1&_=1488782394332

    private List<String> getLink(){
        List<String> stringList = new ArrayList<String>();
        stringList.add("http://chuang.pro/index/list?orderby=hot&index=");
        stringList.add("http://chuang.pro/index/list?orderby=new&index=");
        return stringList;
    }
    public  List<Object> getM(String json) {
        Map<String, Object> map;
        map = JsonUtil.getResult(json);
        if (map == null || map.size() == 0) {
            return null;
        }
       List<Object> objectList = (List) map.get("data");
        if(ListUtil.isEmpty(objectList)){
            logger.info(json);
            return null;
        }
        return objectList;
    }


    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        int totalAll = 0;
        Long time = DateUtil.getUnixTime();
        String date = "&_="+time;
        List<String> stringList = getLink();
        for(String src : stringList){
            int page = 1;
            while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
                String json = WebUtil.buildRequest(src+page+date,false);
                if(StringUtils.isBlank(json)){
                    return null;
                }
                List<Object> arrayList = getM(json);
                if (ListUtil.isEmpty(arrayList) && page == 1) {
                    return error(CrawlConstant.MSG_WARN_NO_CRAWL_ARTICLE);
                } else if (ListUtil.isEmpty(arrayList) && page != 1) {
                    break;
                }
                JsonResult result = iterate2(authorId, arrayList);
                if (!result.isSuccess()) {
                    return result;
                }
                totalAll += result.getInt();
                //不能限制19，应该限制为1，只要此页添加成功一条都得继续下页爬取
                if (!all && result.getInt() < 1) {
                    break;
                }
                page++;
            }
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }
    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate2(int authorId, List< Object> mapList) {
        int succ = 0;
        try {
            Map<String,Object> map ;
            for (Object object : mapList) {
                map = (Map)object;
                String title = (String)map.get("title");
                if (StringUtils.isBlank(title)) {
                    continue;
                }
                title = WebUtil.URLDecode(title);
                // 校验作者、标题是否已存在
                Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
                if (article != null) {
                    continue;
                }
                String link = "http://tech2ipo.com/" + (Integer)map.get("id");
                JsonResult result = parse(authorId, link);
                succ += result.isSuccess() ? 1 : 0;
            }
        } catch (Exception e) {
            logger.error("Parser.parse error,{}", e);
            return error(CommonConstant.ABNORMAL_MSG);
        }
        return success(succ);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return chuangZhiParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return chuangZhiParser.getBody(doc);
    }
}
